package matlap.client.view.admin;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

import matlap.client.services.task.TaskService;
import matlap.client.services.task.TaskServiceAsync;
import  matlap.shared.Task;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;

public class TasksComposite extends Composite {

	TaskServiceAsync c1 = GWT.create(TaskService.class);
	private VerticalPanel vp;
	private Grid g;
	Panel gridPanel = new VerticalPanel();
	private List<Task> tasksList;
	private Panel bigPanel;
	private Label lblNewLabel_1;
	private Label lblNewLabel_2;
	private Label lblNewLabel_3;
	private Label lblNewLabel_4;
	private Label lblNewLabel_5;
	
	public TasksComposite(String w, Panel bp)
	{
		bigPanel = bp;
		vp = new VerticalPanel();
		vp.setSize("689px", "463px");
		initGrid();
		bigPanel.add(vp);
	}

	private void initGrid() 
	{

		g = new Grid(1,6);
		g.setBorderWidth(2);
		gridPanel.add(g);
		vp.add(gridPanel);
		g.setSize("688px", "34px"); 
		
		Label lb0 = new Label("Megnyit\u00E1s");
		lb0.setStyleName("Header");
		g.setWidget(0, 0, lb0);
		
		lblNewLabel_1 = new Label("Felhaszn\u00E1l\u00F3n\u00E9v");
		lblNewLabel_1.setStyleName("Header");
		g.setWidget(0, 1, lblNewLabel_1);
		
		lblNewLabel_2 = new Label("N\u00E9v");
		lblNewLabel_2.setStyleName("Header");
		g.setWidget(0, 2, lblNewLabel_2);
		
		lblNewLabel_3 = new Label("Lapsz\u00E1m (hat\u00E1rid\u0151)");
		lblNewLabel_3.setStyleName("Header");
		g.setWidget(0, 3, lblNewLabel_3);
		
		lblNewLabel_4 = new Label("Bek\u00FCld\u00E9s ideje");
		lblNewLabel_4.setStyleName("Header");
		g.setWidget(0, 4, lblNewLabel_4);
		
		lblNewLabel_5 = new Label("Ki van jav\u00EDtva");
		lblNewLabel_5.setStyleName("Header");
		g.setWidget(0, 5, lblNewLabel_5);


		c1.getTasksNumber(new AsyncCallback<Integer>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Gond van");

			}

			@Override
			public void onSuccess(Integer result) {
				System.out.println(result+" taskscomposite");
				g.resize(result+1,6);
			}			
		});

		c1.getTasks(new AsyncCallback<List<Task>>() {

			public void onFailure(Throwable caught) {
				Window.alert("Gond van ");

			}

			@SuppressWarnings("deprecation")
			public void onSuccess(List<Task> result) {
				
				int i = 1;
				tasksList = new ArrayList<Task>();
				Iterator<Task> it = result.iterator();
				while (it.hasNext()){
					Task task = (Task) it.next();
					tasksList.add(task);
					
					final Hyperlink numberlink = new Hyperlink(i+"", false, "newHistoryToken");
					numberlink.addClickHandler(new ClickHandler(){

						public void onClick(ClickEvent event) {
							int i = Integer.parseInt(numberlink.getText());
							bigPanel.clear();
							Frame fr = new Frame();
							fr.setSize("140%", "200%");
							fr.setUrl(tasksList.get(i-1).getTaskText());
							bigPanel.add(fr);
						}
						
					});
					g.setWidget(i, 0, numberlink);
					g.setText(i, 1, task.getUserName());					
					g.setText(i, 2, task.getName());
					g.setText(i, 3, task.getVolume()+" ("+task.getDeadline()+")");
					g.setText(i, 4, task.getReceived());
					if (task.getCorrected()==1){
						g.setText(i, 5,  "Kijav\u00EDtva");
					}
					else{
						g.setText(i, 5, "Nincs kijav\u00EDtva");
					}
					i++;			
				}	
			}
		});;

	}

}